/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: QueueingConsumer
 * Author:   cakin
 * Date:     2020/5/12
 */
package com.rabbitmq.basicqos;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.util.ConnectionUtils;
import lombok.extern.slf4j.Slf4j;

import static com.rabbitmq.common.Constant.NUM2;

/**
 * @ClassName: QueueingConsumer
 * @Description: QueueingConsumer测试
 * @Date: 2020/5/12
 * @Author: cakin
 */

@Slf4j
public class QueueingConsumerTest {
    /**
     * 消费队列
     */
    private static final String QUEUEING_QUEUE_NAME = "queue.basicqos";

    public static void main( String[] args ) throws Exception {

        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        // 测试basicQos对QueueingConsumer性能的影响，通过开启和屏蔽下面这行来观测
        // channel.basicQos(3);
        // 创建一个QueueingConsumer类型的消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUEING_QUEUE_NAME, false, consumer);
        log.info(" [x] Awaiting requests");

        while (true) {
            // 如果没收到消息，会阻塞
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();

            String message = new String(delivery.getBody());
            log.info(message);
            // 手动发送确认消息
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}

